package com.ruibang.glass.equipment.controller;

import com.ruibang.glass.equipment.constant.Constants;
import com.ruibang.glass.equipment.domain.req.device.PointCheckStandardManagementPageReq;
import com.ruibang.glass.equipment.entity.device.PointCheckStandardManagement;
import com.ruibang.glass.equipment.entity.device.ScrapReport;
import com.ruibang.glass.equipment.service.PointCheckStandardManagementService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.seata.common.util.CollectionUtils;
import io.seata.common.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * <p>
 *点检标准
 * </p>
 *
 * @author mameng
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/api/equipment/point/check/standard/manage")
@Api(value = "点检标准")
public class PointCheckStandardManagementController {
    @Autowired
    private PointCheckStandardManagementService pointCheckStandardManagementService;

    @PostMapping("list")
    @ApiOperation(value = "点检标准列表", response = ResultBody.class)
    public ResultBody getBoilerList(@RequestBody PointCheckStandardManagementPageReq pointCheckStandardManagementPageReq) {
        return ResultBody.success(pointCheckStandardManagementService.getPointCheckStandardManagementList(pointCheckStandardManagementPageReq));
    }

    @PostMapping("addOrUpdateCheck")
    @ApiOperation(value = "修改删除", response = ResultBody.class)
    public ResultBody addChild(@RequestBody PointCheckStandardManagement pointCheckStandardManagement) {
        return ResultBody.success(pointCheckStandardManagementService.addOrUpdateCheck(pointCheckStandardManagement));
    }

    @GetMapping("/getOne/{id}")
    @ApiOperation(value = "查询实体", response = ResultBody.class)
    public ResultBody getOne(@PathVariable("id") String id) {
        return ResultBody.success(pointCheckStandardManagementService.getById(id));
    }


    //
    @PostMapping("deleteAll")
    @ApiOperation(value = "删除", response = ResultBody.class)
    public ResultBody deleteAll(@RequestBody PointCheckStandardManagement pointCheckStandardManagement) {
        if (!CollectionUtils.isEmpty(pointCheckStandardManagement.getIds())) {
            pointCheckStandardManagementService.lambdaUpdate().set(PointCheckStandardManagement::getIsDelete, Constants.ONE_STR).in(PointCheckStandardManagement::getId, pointCheckStandardManagement.getIds()).update();
        }
        if (!StringUtils.isEmpty(pointCheckStandardManagement.getId())) {
            pointCheckStandardManagementService.lambdaUpdate().set(PointCheckStandardManagement::getIsDelete, Constants.ONE_STR).eq(PointCheckStandardManagement::getId, pointCheckStandardManagement.getId()).update();
        }
        return ResultBody.success();
    }

    // 导入
    @PostMapping("importExcel")
    public ResultBody importExcel(@RequestPart("files") MultipartFile file, HttpServletRequest request) {
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String contentType = file.getContentType();
        System.out.println("文件类型是===========" + contentType);
        String fileName = file.getOriginalFilename();
        System.out.println("文件名字是===========" + fileName);
        try {
//            if (!fileName.endsWith(".xlsx")) {
//                return ResultBody.fail(300, "文件格式不正确！");
////                throw new IllegalArgumentException("文件格式不正确！");
//            }
            if (file.isEmpty()) {
                return ResultBody.fail(300, "文件为空！");
//                throw new Exception("文件为空！");
            }
            //根据路径获取这个操作excel的实例
            XSSFWorkbook wb = new XSSFWorkbook(inputStream);
            //根据页面index 获取sheet页
            Sheet sheet = wb.getSheetAt(0);
            //获取sheet页共有多少行
            int totalRos = sheet.getPhysicalNumberOfRows();


            System.out.println("=========总行数为：" + totalRos);
            String deviceName = extractDeviceName(sheet.getRow(1).getCell(0).toString());
            String depart = "";
            for (int i = 4; i < totalRos; i++) {
//                int lastCellNum = sheet.getRow(i).getLastCellNum();
                System.out.println("=========第" + i + "行的数据如下：=========");
                PointCheckStandardManagement pointCheckStandardManagement = new PointCheckStandardManagement();
                if (StringUtils.isNotBlank(sheet.getRow(i).getCell(1).toString())) {
                    depart = sheet.getRow(i).getCell(1).toString();
                }
                if (StringUtils.isBlank(sheet.getRow(i).getCell(2).toString())) {
                    return ResultBody.success();
                }
                pointCheckStandardManagement.setDeviceName(deviceName);
                pointCheckStandardManagement.setPartId(depart);
                pointCheckStandardManagement.setPartName(sheet.getRow(i).getCell(2).toString());
                pointCheckStandardManagement.setInspectionContent(sheet.getRow(i).getCell(3).toString());
                pointCheckStandardManagement.setDedicated(sheet.getRow(i).getCell(4).toString());
                pointCheckStandardManagement.setProduction(sheet.getRow(i).getCell(5).toString());
                pointCheckStandardManagement.setInspectionDivision(sheet.getRow(i).getCell(6).toString());
                if (StringUtils.isNotBlank(sheet.getRow(i).getCell(8).toString())) {
                    System.out.println(sheet.getRow(i).getCell(8));
                    pointCheckStandardManagement.setStatus("运行");
                }
                if (StringUtils.isNotBlank(sheet.getRow(i).getCell(9).toString())) {
                    pointCheckStandardManagement.setStatus("停止");
                }
                if (StringUtils.isNotBlank(sheet.getRow(i).getCell(10).toString())) {
                    pointCheckStandardManagement.setCheckMethod("目视");
                }
                if (StringUtils.isNotBlank(sheet.getRow(i).getCell(11).toString())) {
                    String method = pointCheckStandardManagement.getCheckMethod() + "," + "手摸";
                    pointCheckStandardManagement.setCheckMethod(method);
                }
                if (StringUtils.isNotBlank(sheet.getRow(i).getCell(12).toString())) {
                    String method = pointCheckStandardManagement.getCheckMethod() + "," + "听音";
                    pointCheckStandardManagement.setCheckMethod(method);
                }
                if (StringUtils.isNotBlank(sheet.getRow(i).getCell(13).toString())) {
                    String method = pointCheckStandardManagement.getCheckMethod() + "," + "敲打";
                    pointCheckStandardManagement.setCheckMethod(method);
                }
                if (StringUtils.isNotBlank(sheet.getRow(i).getCell(14).toString())) {
                    String method = pointCheckStandardManagement.getCheckMethod() + "," + "嗅觉";
                    pointCheckStandardManagement.setCheckMethod(method);
                }
                if (StringUtils.isNotBlank(sheet.getRow(i).getCell(15).toString())) {
                    String method = pointCheckStandardManagement.getCheckMethod() + "," + "解体";
                    pointCheckStandardManagement.setCheckMethod(method);
                }
                if (StringUtils.isNotBlank(sheet.getRow(i).getCell(16).toString())) {
                    String method = pointCheckStandardManagement.getCheckMethod() + "," + "测温枪";
                    pointCheckStandardManagement.setCheckMethod(method);
                }
                if (StringUtils.isNotBlank(sheet.getRow(i).getCell(17).toString())) {
                    String method = pointCheckStandardManagement.getCheckMethod() + "," + "测振仪";
                    pointCheckStandardManagement.setCheckMethod(method);
                }
                if (StringUtils.isNotBlank(sheet.getRow(i).getCell(18).toString())) {
                    String method = pointCheckStandardManagement.getCheckMethod() + "," + "其他";
                    pointCheckStandardManagement.setCheckMethod(method);
                }
                pointCheckStandardManagement.setStandard(sheet.getRow(i).getCell(19).toString());
                pointCheckStandardManagementService.save(pointCheckStandardManagement);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultBody.success("成功");

    }


    public static String extractDeviceName(String input) {
        // 正则表达式匹配"设备名称："后面的文本，直到遇到空白字符或字符串结束
        String regex = "设备名称：([^\\s]+)";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);

        if (matcher.find()) {
            // 如果找到匹配项，返回匹配的设备名称
            return matcher.group(1);
        } else {
            // 如果没有找到匹配项，返回空字符串或抛出异常
            return "";
        }
    }
}
